package com.zhengxing.jienci.redis.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

/**
 * @author 浪遏飞舟
 * @create 2020-06-16 16:19
 */
public class BeanUtils {

    private static final ObjectMapper mapper = new ObjectMapper();

    // 将对象转成字符串
    public static String objectToString(Object obj) throws Exception {
        return mapper.writeValueAsString(obj);
    }
    // 将Map转成指定的Bean
    public static Object mapToBean(Map map, Class clazz) throws Exception {
        return mapper.readValue(objectToString(map), clazz);
    }
    // 将Bean转成Map
    public static Map beanToMap(Object obj) throws Exception {
        return mapper.readValue(objectToString(obj), Map.class);
    }

}
